package cn.rxmind.dlicense.core.constants;

import cn.rxmind.dlicense.core.annotation.Displayable;
import com.google.common.collect.Sets;

import javax.security.auth.x500.X500Principal;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 证书上下文,一些与业务关联性不大的字段，在这里设置默认值即可，如后期有要求变动则可在外部覆盖
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/12 2:26 下午
 */
public final class DLicenseConstants {

   public final static Map TYPE_PARAM = new HashMap(16);

    static {

        Set<Class<? extends Enum>> classSet = Sets.newHashSet(CheckRuleEnum.class);
        classSet.stream().filter(clazz -> clazz.isEnum() && clazz.getAnnotation(Displayable.class) != null)
                .forEach(clazz ->{
                    String simpleName = clazz.getSimpleName();
                    List<Map<String,Object>> valueList = new ArrayList<>();
                    try {
                        Object[] values = (Object[])clazz.getMethod("values").invoke(null);
                        for (Object value : values) {
                            Map<String, Object> element = new HashMap<>();
                            Object name = clazz.getMethod("name").invoke(value);
                            Object getName = clazz.getMethod("getName").invoke(value);
                            element.put("name",name);
                            element.put("displayName",getName);
                            valueList.add(element);
                        }

                    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                        e.printStackTrace();
                    }

                    TYPE_PARAM.put(simpleName,valueList);
                });
    }

    public static final X500Principal DEFAULT_HOLDER_AND_ISSUER
            = new X500Principal("CN=littleutry, OU=utry, O=hy, L=HZ, ST=ZJ, C=CN");

    // 证书内容部分，这些常量固定以降低复杂性

    public static final String CONSUMER_TYPE = "user";
    public static final int CONSUMER_AMOUNT = 1;
    public static final String INFO = "证书文件";
    public static final String SUBJECT = "renxin";

    // 私钥配置

    public static final String PRIVATE_STORE_PATH = "/license/privateKeys.keystore";
    public static final String PRIVATE_ALIAS = "privateKey";

    // 公钥配置

    public static final String PUBLIC_ALIAS = "publicCert";
    public static final String PUBLIC_STORE_PATH = "/license/publicCerts.keystore";

    // 密钥库密码

    public static final String STORE_PWD = "renxin123";
    public static final String KEY_PWD = "renxin123";

}
